package usr

import (
	"sync"
)

type BaseUsrService struct {
	Dao *BaseUsrDao
}

var (
	instanceBaseUsrService *BaseUsrService
	onceBaseUsrService     sync.Once
)

func NewBaseUsrService() *BaseUsrService {
	onceBaseUsrService.Do(func() {
		instanceBaseUsrService = &BaseUsrService{Dao: NewBaseUsrDao()}
	})
	return instanceBaseUsrService
}

// 创建 base_usr
func (usr *BaseUsrService) CreateUsr(model *BaseUsr) (*BaseUsr, error) {
	id, err := usr.Dao.CreateBaseUsr(model)
	if err != nil {
		return &BaseUsr{}, err
	}
	model.Id = id
	return model, nil
}

// 根据 id 删除 base_usr
func (usr *BaseUsrService) DeleteUsrById(id *BaseUsrId) error {
	if err := usr.Dao.DeleteBaseUsrById(id); err != nil {
		return err
	}

	return nil
}

// 根据 id 修改 base_usr
func (usr *BaseUsrService) UpdateUsrById(id *BaseUsrId, model *BaseUsr) error {
	if err := usr.Dao.UpdateBaseUsrById(id, model); err != nil {
		return err
	}

	return nil
}

// 查询全部 base_usr
func (usr *BaseUsrService) FindAllUsr() (*[]BaseUsr, error) {
	return usr.Dao.FindAllBaseUsr()
}
